AddSaleOrderWindow = Ext.extend(Ext.Window, {
//	constructor:function(config){
//		config=Ext.apply({
//		  layout:'fit'
//		},config);
//		AddSaleOrderWindow.superclass.constructor(config);
//	},
//	setSaleGridHeight : function(height) {
//		this.sale_item_grid.setHeight(height);
//	},
//	setSalePanelHeight : function(height) {
//		this.sale_panel.setHeight(height);
//	},
//	setJoinPanelHeight : function(height) {
//		this.joinPanel.setHeight(height);
//	},
	initPanel : function(initParams) {
		
		this.createAllHidden();
		this.createAllComb();
		
		this.sale_item_colName = this.createSaleItemColName();
		this.sale_item_record = this.createSaleItemRecord();
		this.sale_item_store = this.createSaleItemStore();
		this.sale_item_grid = this.createSaleItemGrid();
		
		this.partner_combox = this.createPartnerComb();
		this.address_combox = this.createAddressCmb();
				
		this.sale_panel = this.createPanel();
//		this.joinPanel = new Ext.Panel({
//			layout:'fit',
//			flex:1,
//			frame : true,
////			height : this.joinPanelHeight,
//			autoScroll : true,
//			items : [this.sale_panel]
//		});

//		this.setSaleGridHeight(300);
//		this.setSalePanelHeight(450);
//		this.setJoinPanelHeight(700);
		
		this.add(this.sale_panel);
		this.captureSaleStoreEvent2();
		
		this.on('activate', function() {
			if( !this.myLoaded ) {
				this.loadContent(initParams);
				this.myLoaded = true;
			}
		});
		
	},
//	captureSaleStoreEvent : function() {
////		alert('1');
//		Ext.util.Observable.capture(this.sale_item_store, function(e) {
//			if(e == 'load') {
//				this.addRemoveEvent();
//			}
//		}.createDelegate(this));
//	},
	captureSaleStoreEvent2 : function() {
//		alert('2');
		Ext.util.Observable.capture(this.sale_item_grid.getView(), function(e) {
			if (e == 'rowsinserted' || e == 'rowupdated'
							|| e == 'rowremoved') {
				this.sumAmount();
//				this.addRemoveEvent();
			}
		}.createDelegate(this));
	},
	loadContent : function(initParams) {
		var virtual_btn = new this.buttonModule();
		virtual_btn.main_win = this;
		
//		console.debug(initParams);
//		alert( Ext.decode(initParams.jsArgs) );
		
		if(initParams.jsArgs ) {
//			this.noticeId = initParams.id;
//			this.noticeWin = this.creator.app.getDesktop.getWindow('notice');
			initParams.id = Ext.decode(initParams.jsArgs).id;
		}
		
		if(initParams.id) {
			
			virtual_btn.url = this.initShowUrl;
			virtual_btn.alertMeses = {
				'saleOk' : PurChaseAlert.PROMOTE_OK + ':订单查询成功',
				'saleNotCurrentUser' : PurChaseAlert.PROMOTE_OK
						+ ':订单查询成功，您不是当前处理人',
				'saleSubmit' : PurChaseAlert.PROMOTE_OK + ':订单查询成功，该单据已被提交',
				'saleNotExist' : PurChaseAlert.PROMOTE_ERROR
						+ ':订单查询失败,该单据不存在或已被删除',
				'saleError' : PurChaseAlert.PROMOTE_ERROR + ':订单查询失败',
				'saleTakeOver' : PurChaseAlert.PROMOTE_OK + ':订单查询成功,请处理'
			};
			virtual_btn.sendDataAndGetMes({
//				status : initParams.init_status,
				saleOrderId : initParams.id
			}, true);
		} else {
			virtual_btn.url = RES_PATH + '/saleOrder/toCreate.do';
			virtual_btn.sendDataAndGetMes(null, true);
		}
	},
	setSellerCode : function(code) {
		this.sellerCode = code;
		this.setTitle(this.creator.ordersName + code);
	},
	createPanel : function() {
		var sale_formReader = new Ext.data.JsonReader({
			root : ""
				// totalProperty : 'totalProperty'
			}, [{
					name : 'id'
				}, {
					name : 'workFlowId'
				}, {
					name : 'codeOfSeller'
				}, {
					name : 'buyerId'
				}, {
					name : 'areaId'
				}, {
					name : 'buyerName'
				}, {
					name : 'memoOfSeller'
				}, {
					name : 'memoOfBuyer'
				}, {
					name : 'street'
				}, {
					name : 'receiverName'
				}, {
					name : 'postCode'
				}, {
					name : 'phone'
				}, {
					name : 'mobile'
				}, {
					name : 'province'
				}, {
					name : 'city'
				}, {
					name : 'district'
				}, {
					name : 'paymentType'
				}, {
					name : 'transportType'
				}, {
					name : 'statusDes'
				}, {
					name : 'status'
				}, {
					name : 'creator'
				}, {
					name : 'gmtCreate'
				}, {
					name : 'submitter'
				}, {
					name : 'gmtSubmit'
				}, {
					name : 'amount'
				}, {
					name : 'pyCode'
				}, {
					name : 'partnerNameBySeller'
				}, {
					name : 'partnerIdBySeller'
				}, {
					name : 'partnerPyCodeBySeller'
				}]);

		
				
		var sale_form_title = new Ext.Container({

					
						style : 'padding:0px;fontSize:13px;',
						layout : 'uec3grid',
						// width : '95%',
						height : 32,
						layoutConfig : {
							hGrid:[33,33,14,5,14,1],
            				vGrid:[20,70,8,4]
						},
						items : [{
									xtype : 'label',
									text : this.creator.ordersName,
									style : {
										font : 'bold 18px arial, serif',
										textAlign : 'center'
									},
									grid : [2, 2, 2, 2]
									
								}, {
									xtype : 'label',
									text : '单据号:',
									style : {
										fontSize : '13px'
									},
									grid : [4, 2, 4, 2]
								}, {
									xtype : 'textfield',
									name : 'codeOfSeller',
									style : PurchaseColor.READ_ONLY_COLOR
											,
									grid : [5, 2, 5, 2],
									readOnly : true
								},
								{
					              html : '<hr color="#34A4E0" />',
					              grid:[1,4,6,4]
					             }
								]
					});
					
		var sale_form_item1 = new Ext.Container({
					layout : 'uec3grid',
						// width : '95%',
					height : 75,
					layoutConfig : {
							hGrid:[8,17,8,17,8,17,8,17],
            				vGrid:[5,31,31,31,2],
            				usedGrid:[[1,1,8,1]]
					},
						
					items : [{
									xtype : 'label',
									text : '采购单位:',
									style : {
										marginLeft : '1px',
										width : '81px'
									}
								}, Ext.apply(this.partner_combox,{grid:[2,2,4,2]}), this.sale_id_hidden,
								this.sale_buyer_id_hidden, this.sale_wkflId_hidden,
								this.sale_areaId_hidden,
								this.sale_province_hidden,
								this.sale_city_hidden,
								this.sale_district_hidden,
								this.sale_street_hidden,
								this.sale_receiverName_hidden,
								this.sale_postCode_hidden,
								this.sale_phone_hidden,
								this.sale_mobile_hidden,
								this.sale_status_hidden, this.sale_partnerPyCodeBySeller_hidden
								, {
									xtype : 'label',
									text : '支付方式:  '
								}, this.payType_cmb, {
									xtype : 'label',
									text : '运输方式:  '
								}, this.transportType_cmb, {
									xtype : 'label',
									text : '收货地址:  '
									
								}, Ext.apply(this.address_combox,{grid:[2,3,8,3]})
								, {
									xtype : 'label',
									text : '卖方备注:'
								}, {
									xtype : 'textarea',
									name : 'memoOfSeller',
									grid:[2,4,4,4]
								},  {
									xtype : 'label',
									text : '买方备注:'
								}, {
									xtype : 'textarea',
									name : 'memoOfBuyer',
									style : PurchaseColor.READ_ONLY_COLOR,
									readOnly : true,
									grid:[6,4,8,4]
								
								}]
					
				});
		var maindata_item= new Ext.Container({
           layout:'vbox',
           flex:1,					
                    layoutConfig: {
                       align : 'stretch',
                       pack  : 'start'
                    },
            items:[
           sale_form_item1
		  , this.sale_item_grid]
	   });
		var sale_form_item2 = {
			style : 'padding:0px; fontSize:13px; marginTop:3px',
			layout : 'uec3grid',
			height : 28,
			layoutConfig : {
				hGrid:[7,10,6,10,7,10,6,10,7,10,7,10],
				vGrid:[10,80,10],
				usedGrid:[1,1,12,1]
			},
			items : [{
						xtype : 'label',
						text : '单据状态:',
						style : {
							marginLeft : '1px',
							width : '70px'
						}
					}, {
						xtype : 'textfield',
						name : 'statusDes',
						style : PurchaseColor.READ_ONLY_COLOR
								+ "; width : 100px",
						readOnly : true
					}, {
						xtype : 'label',
						text : '创建人:',
						style : {
							marginLeft : '5px',
							width : '60px'
						}
					}, {
						xtype : 'textfield',
						name : 'creator',
						style : PurchaseColor.READ_ONLY_COLOR
								+ "; width : 80px",
						readOnly : true
					}, {
						xtype : 'label',
						text : '创建时间:',
						style : {
							marginLeft : '5px',
							width : '70px'
						}
					}, {
						xtype : 'textfield',
						name : 'gmtCreate',
						style : PurchaseColor.READ_ONLY_COLOR
								+ "; width : 90px",
						readOnly : true
					}, {
						xtype : 'label',
						text : '提交人:',
						style : {
							marginLeft : '5px',
							width : '60px'
						}
					}, {
						xtype : 'textfield',
						name : 'submitter',
						style : PurchaseColor.READ_ONLY_COLOR
								+ "; width : 80px",
						readOnly : true
					}, {
						xtype : 'label',
						text : '提交时间:',
						style : {
							marginLeft : '5px',
							width : '70px'
						}
					}, {
						xtype : 'textfield',
						name : 'gmtSubmit',
						style : PurchaseColor.READ_ONLY_COLOR
								+ "; width : 90px",
						readOnly : true
					}, {
						xtype : 'label',
						text : '总金额:',
						style : {
							marginLeft : '5px',
							width : '60px'
						}
					}, {
						xtype : 'textfield',
						// id : 'pur_amount',
						name : 'amount',
						style : PurchaseColor.READ_ONLY_COLOR
								+ "; width : 90px",
						readOnly : true
					}]
		};

		// 表单数据
		return new Ext.FormPanel({
//					height : this.salePanelHeight,
					reader : sale_formReader,
					flex:1,
					layout:"vbox",
                    layoutConfig: {
                       align : 'stretch',
                       pack  : 'start'
                    },
					items : [sale_form_title,maindata_item,
							sale_form_item2]
				});

	},

	createAllHidden : function() {
		this.sale_id_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'id'
				});

		this.sale_buyer_id_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'buyerId'
				});

		this.sale_areaId_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'areaId'
				});

		this.sale_province_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'province'
				});

		this.sale_city_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'city'
				});

		this.sale_district_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'district'
				});

		this.sale_street_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'street'
				});

		this.sale_receiverName_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'receiverName'
				});

		this.sale_postCode_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'postCode'
				});

		this.sale_phone_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'phone'
				});

		this.sale_mobile_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'mobile'
				});

		this.sale_status_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'status'
				});

		this.sale_wkflId_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'workFlowId'
				});
				
		this.sale_partnerPyCodeBySeller_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'partnerPyCodeBySeller'
				});
	},

	createAllComb : function() {
		// 支付方式下拉框
		this.payType_store = getDictionaryStore(RES_PATH, {
					group : 'payType'
				});
		this.payType_cmb = getDictionaryComboBox(this.payType_store, 'remote',
				'paymentType', '支付方式', null, 100);

		// 运输方式下拉框
		this.transportType_store = getDictionaryStore(RES_PATH, {
					group : 'transportType'
				});
		this.transportType_cmb = getDictionaryComboBox(
				this.transportType_store, 'remote', 'transportType', '运输方式',
				null, 100);
				
//		this.transportType_cmb1 = Uec3.App.dictionary.getCombo("transportType", {showWhole:true, width:100, hiddenName : 'transportType'});
//		this.payType_cmb1 = Uec3.App.dictionary.getCombo("payType", {showWhole:true, width:100, hiddenName : 'paymentType'});

		// 来源下拉框
		this.sourceType_store = getDictionaryStore(RES_PATH, {
					group : 'part_source'
				});
		this.source_cmb = getDictionaryComboBox(this.sourceType_store, 'local',
				'part_source', '来源', 'source');

		this.sourceType_store.load();
	},
	
	showInventoryById : function(partId) {
//		Ext.Msg.alert('信息', '库存数量:XXX');
		var desktop = Uec3.App.getDesktop();
		desktop.launchWindowByParam('inventory', {'partId' : partId, 'showType' : 1});
	},
	
	createDelPurBtn : function(value, metadata, record, rowIndex, colIndex, store) {
		if(this.status && this.status != '-1' && this.status != '0' && this.status != '2') {
//		alert(this.status);
			return '已确认';
		} 

		var delStr = '';
		if(this.creator.ordersNameEn == 'not_online_sale_order') {
			delStr = '<a style="font-size:12px;" ' + ' href=javascript:getRunningWindow(' + 
				this.creatorIndex + ',Uec3.AddAutoSaleOrderWindowCreator).delSaleRecordRow("' + 
				record.id + '"); >删除</a>';
		}
				
		var showInventoryStr = '<a style="font-size:12px;" ' + ' href=javascript:getRunningWindow(' + 
				this.creatorIndex + ',Uec3.AddAutoSaleOrderWindowCreator).showInventoryById("' + 
				record.data.partId + '"); >查看库存</a>';
		return delStr + '&nbsp;&nbsp;' + showInventoryStr;
	},
//	getSendQty : function(value, metadata, record, rowIndex, colIndex, store) {
//		if( !value) {
//			return 0;
//		}
//	},
//	getNotSendQty : function(value, metadata, record, rowIndex, colIndex, store) {
//		if( !value) {
//			return record.data.orderQuantity;
//		}
//	},
	createSaleItemColName : function() {
		
		this.orderQuantityEditor = new Ext.form.NumberField({
			regex : /([1-9][0-9]*(\.\d+)?)|(0\.\d+)/,
			regexText : '您只能够输入数字'
		});
		
		this.quantityEditor = new Ext.form.NumberField({
			regex : /([1-9][0-9]*(\.\d+)?)|(0\.\d+)/,
			regexText : '您只能够输入数字'
		});
		
		// 定义grid表头
		return new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
				{
					header : '操作',
					id : 'exec',
					dataIndex : 'id',
					renderer : this.createDelPurBtn.createDelegate(this)
				}, {
					header : '订购数量',
					dataIndex : 'orderQuantity',
					editor : this.orderQuantityEditor
				}, {
					header : '销售数量',
					dataIndex : 'quantity',
					editor : this.quantityEditor
//					renderer : this.getSendQty.createDelegate(this)
				}, {
					header : '零件编码',
					dataIndex : 'code'
				}, {
					header : '零件名称',
					dataIndex : 'name'
				}, {
					header : '来源',
					dataIndex : 'source',
					renderer : getSourceName.createDelegate(this)
//					editor : this.source_cmb
				}, {
					header : '单位',
					dataIndex : 'measurementUnit'
				}, {
					header : '品牌',
					dataIndex : 'brand',
					sortable : true
				}, {
					header : '产地',
					dataIndex : 'origin',
					sortable : true
				}, {
					header : '媒体价',
					dataIndex : 'retailPrice'
				}, {
					header : '折扣',
					dataIndex : 'discount_show',
					renderer : function(value, cellmeta, record, rowIndex, columnIndex, store) {
						return record.get('discount') + '%';
					}.createDelegate(this)
				}, {
					header : '折后价',
					dataIndex : 'price'
				}]);

	},

	createSaleItemRecord : function() {
		return Ext.data.Record.create([{
					name : 'id'
				}, {
					name : 'code'
				}, {
					name : 'buyerId'
				}, {
					name : 'buyerName'
				}, {
					name : 'name'
				}, {
					name : 'source'
				}, {
					name : 'measurementUnit'
				}, {
					name : 'brand'
				}, {
					name : 'origin'
				}, {
					name : 'retailPrice'
				}, {
					name : 'discount'
				}, {
					name : 'discount_show'
				}, {
					name : 'price'
				}, {
					name : 'orderQuantity'
				}, {
					name : 'quantity'
				}, {
					name : 'level1Id'
				}, {
					name : 'partId'
				}, {
					name : 'level1Name'
				}, {
					name : 'level2Name'
				}, {
					name : 'partCategoryId'
				}, {
					name : 'durability'
				}, {
					name : 'inventoryIdBySeller'
				}, {
					name : 'exec'
				}]);

	},

	createSaleItemStore : function() {
		return new Ext.data.JsonStore({
					url : this.loadDetailsUrl,
					baseParams : {},
					mode : "local",
					totalProperty : "results",
					root : "saleDetails",
					idProperty : "id",
					pruneModifiedRecords : true,
					fields : this.sale_item_record
				});
	},
	createSales : function() {
		this.creator.app.getDesktop().launchWindowByParam('sale_add', {'orderId' : this.sale_id_hidden.getValue(), 'codeOfSeller' : this.sale_panel.getForm().findField('codeOfSeller').getValue()});
	},
	createSaleItemGrid : function() {
		
		this.saleOrder_toolbar = new Ext.Toolbar({
			  items:[]
		});
		
		// 创建grid，装载明细数据
		return new Ext.grid.EditorGridPanel({
			style : {padding : '2px'},
			flex:1,
//			height : this.saleGirdHeight,
			clicksToEdit : 1,
			viewConfig : {
				forceFit : true
			},
			colModel : this.sale_item_colName,
			store : this.sale_item_store,
			tbar : this.saleOrder_toolbar
		});
	},
	
	createPartnerComb : function() {
		this.ds_partner_store = new Ext.data.JsonStore({
					url : RES_PATH + '/partner/query_partner!queryPartner.do',
					id : "id",
					root : 'root',
					totalProperty : 'totalProperty',
					remoteSort : true,
					baseParams : {type : 0},
					fields : [{
								name : 'id', 
								type : 'int'
							}, {
								name : 'pyCode',
								type : 'string'
							}, {
								name : 'name', 
								type : 'string'
							}, {
								name : 'companyId',
								type : 'int'
							}]
				});

		/**
		 * 采购商combox
		 */
		return new Ext.form.LookupBox2({
					store : this.ds_partner_store,
					displayField : 'name',
					lookupField : 'pyCode',
					valueField : 'id',
					hiddenId : 'partnerIdBySeller' + this.versionCode,
					hiddenName : 'partnerIdBySeller',
					typeAhead : false,
					triggerAction : 'all',
					emptyText : '输入拼音或名字查询采购商..',
					selectOnFocus : true,
					queryParam : 'queryParam',
					queryDelay : 10,
					typeAheadDelay : 10,
//					lookupCallBackEvents:['lookupChange'],
					pageSize : 4,
					start : 0,
					anchor : '100%',
					width : 318,
					app : this.creator.app,
					triggerTags : [{
							moduleId : 'partner_query',
							params : {'partnerType' : 0},
							triggerClass : 'x-form-search-trigger'
						}, {
							moduleId : 'partner_add',
							params : {'partner_index' : 1}
						}],
					cleanValueTrigger : {
						triggerHandler : function() {
							this.readyToClearValue();
						},
						triggerClass : 'x-form-clear-trigger'
					},
					onSelect : function(record, index) {
						if (this.fireEvent('beforeselect', this, record, index) !== false) {
//							this.setValue(record.data[this.valueField], record.data[this.lookupField]);
							this.collapse();
							this.fireEvent('select', this, record, index);
						}
					},
					listeners : {
						'select' : function(lookup, record, index) {
							this.selectPartnerCmb(lookup, record, index);
						}.createDelegate(this)
						,
						'lookupChange' : function(record, creator, oldValue, win) {
							this.lookupChangePartnerCmb(record, creator, oldValue, win);
							
						}.createDelegate(this)
						,
						'lookupClear' : function() {
							this.clearPartnerCmb();
							
						}.createDelegate(this)
					}
				});
	},
	lookupChangePartnerCmb : function(record, creator, oldValue, win) {
		if(oldValue && (oldValue + '').trim() != '' && record.data.id != oldValue ) {
			var count = this.sale_item_store.getCount();
			if(count > 0) {
   				Ext.Msg.confirm("提示", '更改采购单位将删除原有零件记录，您确定修改吗？', function(btnId) {
   					if(btnId == 'no') {
						return;
					} else {
				   	    creator.setLookupValue(record.data, win);
				   	    this.sale_item_store.removeAll();
						this.address_combox.clearValue();
				   	    
//				   	    this.sale_buyer_id_hidden.setValue(record.data.companyId);
//				   	    this.sale_partnerPyCodeBySeller_hidden.setValue(record.data.pyCode);
				   	    
//				   	    var partnerId = this.partner_combox.getValue();	
//						this.address_combox.triggerTags[0].params = {partnerId:partnerId};
//						this.address_combox.triggerTags[1].params = {partnerId:partnerId};
					}
		       	}.createDelegate(this));
			} else {
   				creator.setLookupValue(record.data, win);
				this.address_combox.clearValue();
   				
//   			this.sale_buyer_id_hidden.setValue(record.data.companyId);
//   			this.sale_partnerPyCodeBySeller_hidden.setValue(record.data.pyCode);
   				
//   			var partnerId = this.partner_combox.getValue();	
//				this.address_combox.triggerTags[0].params = {partnerId:partnerId};
//				this.address_combox.triggerTags[1].params = {partnerId:partnerId};
			}
		} else if(!oldValue) {
			creator.setLookupValue(record.data, win);
			
//			this.sale_buyer_id_hidden.setValue(record.data.companyId);
//			this.sale_partnerPyCodeBySeller_hidden.setValue(record.data.pyCode);
			
//			var partnerId = this.partner_combox.getValue();	
//			this.address_combox.triggerTags[0].params = {partnerId:partnerId};
//			this.address_combox.triggerTags[1].params = {partnerId:partnerId};
		} else {}
		
	},
	selectPartnerCmb : function(lookup, record, index) {
		if(lookup.getValue() && (lookup.getValue() + '').trim() != '' && lookup.getValue() != record.data.id ) {
			var count = this.sale_item_store.getCount();
			if(count > 0) {
				Ext.Msg.confirm("提示", '更改采购单位将删除原有零件记录，您确定修改吗？', function(btnId) {
					if(btnId == 'no') {
						return;
					} else {
						lookup.setValue(record.data[lookup.valueField], record.data[lookup.lookupField]);
						this.sale_item_store.removeAll();
						this.address_combox.clearValue();
						
//						this.sale_buyer_id_hidden.setValue(record.data.companyId);
//						this.sale_partnerPyCodeBySeller_hidden.setValue(record.data.pyCode);
//						this.address_combox.triggerTags[0].params = {'partnerId' : lookup.getValue()};
//						this.address_combox.triggerTags[1].params = {'partnerId' : lookup.getValue()};
					}
				}.createDelegate(this));
			} else {
				lookup.setValue(record.data[lookup.valueField], record.data[lookup.lookupField]);
				this.address_combox.clearValue();
				
//				this.sale_buyer_id_hidden.setValue(record.data.companyId);
//				this.sale_partnerPyCodeBySeller_hidden.setValue(record.data.pyCode);
//				this.address_combox.triggerTags[0].params = {'partnerId' : lookup.getValue()};
//				this.address_combox.triggerTags[1].params = {'partnerId' : lookup.getValue()};
			}
		} else if(!lookup.getValue()) {
			lookup.setValue(record.data[lookup.valueField], record.data[lookup.lookupField]);
			
//			this.sale_buyer_id_hidden.setValue(record.data.companyId);
//			this.sale_partnerPyCodeBySeller_hidden.setValue(record.data.pyCode);
//			this.address_combox.triggerTags[0].params = {'partnerId' : lookup.getValue()};
//			this.address_combox.triggerTags[1].params = {'partnerId' : lookup.getValue()};
		} else {}
		
	},
	clearPartnerCmb : function() {
		if(!this.partner_combox.getValue()) {
			return;
		}
		var count = this.sale_item_store.getCount();
		if(count > 0) {
			Ext.Msg.confirm("提示", '更改采购单位将删除原有零件记录，您确定修改吗？', function(btnId) {
				if(btnId == 'no') {
					return;
				} else {
					this.partner_combox.clearValue();
					this.sale_item_store.removeAll();
					this.address_combox.clearValue();
					
//					this.sale_buyer_id_hidden.setValue(null);
//					this.sale_partnerPyCodeBySeller_hidden.setValue(null);
					
//					this.address_combox.triggerTags[0].params = null;
//					this.address_combox.triggerTags[1].params = null;
				}
			}.createDelegate(this));
		} else {
			this.partner_combox.clearValue();
			this.address_combox.clearValue();
			
//			this.sale_buyer_id_hidden.setValue(null);
//			this.sale_partnerPyCodeBySeller_hidden.setValue(null);
			
//			this.address_combox.triggerTags[0].params = null;
//			this.address_combox.triggerTags[1].params = null;
		}
		
	},
	
	getModifiedData : function() {
		var obj = {};
		obj.all = [];
		obj.add = [];
		obj.update = [];
		this.sale_item_store.each(function(rec) {
					if (rec.data.id) {
						obj.all.push(rec.data.id);
					} else {
						obj.add.push(rec.data);
					}
				});
		var update = this.sale_item_store.getModifiedRecords();
		Ext.each(update, function(rec) {
					if (rec.data.id) {
						obj.update.push(rec.data);
					}
				});
		return obj;
	},

	removeOldSaleButtons : function() {
		this.sale_panel.remove(SALE_BUTTON_FDSET_ID + this.versionCode, true);
	},

	createNewSaleButtons : function(btns) {
		var btnFieldset = new Ext.Toolbar({
					id : SALE_BUTTON_FDSET_ID + this.versionCode,
					xtype:"toolbar",
			         height:40,
			         buttonAlign :"center"
				});

		this.sale_panel.add(btnFieldset);
		this.sale_panel.doLayout(true);

		 var ref = this;
		for (var i = 0; i < btns.length; i++) {
			var obj = btns[i];
			var btn = new this.buttonModule({
						id : obj.id + this.versionCode,
						text : obj.value,
						width : 80,
//						height : 22,
						url : RES_PATH + obj.url,
						func : obj.func,
						alertMeses : obj.alertMeses,
						tooltip : obj.tips,
						tooltipType : 'qtip',
						handler : function(bn) {
							bn.main_win = ref;
							if(bn.func == 'saleNextOne' || bn.func == 'saleOnlineQuery' || bn.func == 'saleNotOnlineQuery') {
								eval('bn.' + bn.func + '()');
							} else {
								beforeClickBtnAlert.createDelegate(bn, [eval('bn.' + bn.func)])();
							}
						}
					});

			btnFieldset.add(btn);
			btnFieldset.doLayout(true);
		}
	},
	
	validateSaleData : function() {
		var partnerIdBySeller = this.partner_combox.getValue();
		if (!partnerIdBySeller) {
			Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起,您还没有选择采购单位..');
			return false;
		}
		
		if( isRecordNULL(this.sale_item_store) ) {
			Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起,您的行记录不能为空..');
			return false;
		}
		
		var payType = this.payType_cmb.getValue();
		if( payType == null || (payType + '').trim() == '' ) {
			Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起,您还没有选择支付方式..');
			return false;
		}
		
		var transType = this.transportType_cmb.getValue();
		if( transType == null || (transType + '').trim() == '' ) {
			Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起,您还没有选择运输方式..');
			return false;
		}
		
		var address = this.address_combox.getValue();
		if( !address) {
			Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起,您还没有选择收货地址..');
			return false;
		}
		
		return true;
	},

	insertSaleRecord : function() {

	},

	createAddressCmb : function() {
		function fillPartnerAddressInfo(record) {
			var fieldNames = ['areaId', 'province', 'city', 'district',
					'street', 'postCode', 'receiverName', 'phone', 'mobile'];
			Ext.each(fieldNames, function(fieldName, index, array) {
				if(record.data) {
					this.sale_panel.getForm().findField(fieldName).setValue(record.data[fieldName]);
				} else {
					this.sale_panel.getForm().findField(fieldName).setValue(record[fieldName]);
				}
			}.createDelegate(this));
		}

		var ds_address_store = new Ext.data.JsonStore({
//			url : RES_PATH
//					+ "/partnerAddress/query_partnerAddress!queryPartnerAddressImplicit.do",
//			id : "id",
//			root : 'root',
//			totalProperty : 'totalProperty',
//			remoteSort : true,
//			baseParams : {
//				sort : 'id',
//				dir : 'ASC'
//			},
			fields : [{
						name : 'areaId',
						type : 'string'
					}, {
						name : 'province',
						type : 'string'
					}, {
						name : 'city',
						type : 'string'
					}, {
						name : 'district',
						type : 'string'
					}, {
						name : 'street',
						type : 'string'
					}, {
						name : 'postCode',
						type : 'string'
					}, {
						name : 'receiverName',
						type : 'string'
					}, {
						name : 'phone',
						type : 'string'
					}, {
						name : 'mobile',
						type : 'string'
					}, {
						name : 'address',
						type : 'string'
					}],
			listeners : {
				'beforeload' : function(store, options) {

				}
//				,
//				'load' : function(store, records, options) {
//					Ext.each(records, function(record, index, array) {
//								var address = record.data.province
//										+ record.data.city
//										+ record.data.district
//										+ record.data.street;
//								record.set('address', address);
//							});
//				}
			}
		});

		return new Ext.form.LookupBox2({
					store : ds_address_store,
					displayField : 'address',
					lookupField : 'areaId',
					valueField : 'areaId',
					typeAhead : false,
					mode : 'local',
					triggerAction : 'all',
					emptyText : '选择收货地址..',
					selectOnFocus : true,
//					queryParam : 'queryParam',
//					queryDelay : 10,
//					typeAheadDelay : 10,
//					pageSize : 4,
//					start : 0,
					lookupCallBackEvents:['lookupChange'],
					width : 400,
					anchor : '100%',
					editable : false,
					app : this.creator.app,
					triggerTags : [{
								moduleId : 'partnerAddress_query',
								params : {'partnerCmb' : this.partner_combox},
								triggerClass : 'x-form-search-trigger'
							}, {
								moduleId : 'partnerAddress_add',
								params : {'partnerCmb' : this.partner_combox}
							}],
					listeners : {
						'lookupChange' : fillPartnerAddressInfo
								.createDelegate(this)
					}
				});

	},

	sumAmount : function() {
		var recs = this.sale_item_store.getRange();

		var amount = 0.0;

		Ext.each(recs, function(rec) {
					amount += Ext.util.Format.number(rec.data.price)
							* Ext.util.Format.number(rec.data.quantity);
				});
		amount = Ext.util.Format.number(amount, '0.00');
		this.sale_panel.getForm().findField('amount').setValue(amount);
	},

	delSaleRecordRow : function(id) {
		var record = this.sale_item_store.getById(id);
		this.sale_item_store.remove(record);
	},

//	addRemoveEvent : function() {
//		var ref = this;
//		var recs = this.sale_item_store.getRange();
//		Ext.each(recs, function(rec) {
////			alert(SALE_DEL_RECORD_ID + rec.id + this.versionCode);
//			var obj = Ext.get(SALE_DEL_RECORD_ID + rec.id + this.versionCode);
//
//			obj.on('click', function() {
//				ref.delSaleRecordRow(this);
//				
//			}.createDelegate(rec))
//		}.createDelegate(this))
//	},
	
	getFormValues : function() {
		var form_values = this.sale_panel.getForm().getValues();

		for (var j in form_values) {
			form_values[j] = castNoSelToNULL(form_values[j]);
		}

		form_values = Ext.util.JSON.encode(form_values);

		return form_values;
	}

});